package com.valveinspectionsys.repair.activity;

import java.util.ArrayList;
import java.util.List;

import com.valveinspectionsys.R;
import com.valveinspectionsys.dbhelp.XmlNameHelper;
import com.valveinspectionsys.gridview.GridMainActivity;
import com.valveinspectionsys.gridview.QueryRoutePlanMap;
import com.valveinspectionsys.maintain.activity.MaintainDeviceNoRfid;

import com.valveinspectionsys.povo.DeviceVo;
import com.valveinspectionsys.povo.RepairVo;
import com.valveinspectionsys.repair.db.RepairDao;
import com.valveinspectionsys.routing.activity.DeviceInfoActivity;
import com.valveinspectionsys.util.SharedPreferenceUtil;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


/**
 * 维修设备信息
 * @author King
 *2013-4-27
 */
public class RepairDeviceInfoActivity extends Activity {
    public static final int ITEM0 = Menu.FIRST;// 系统值
    public static final int REQUEST_CODE_BANDING=1;//绑定
    public static final int REQUEST_CODE_NORFID=2;//异常巡检
    public TextView textview1;//设备名称
    public TextView textview2;//设备编号
    public TextView textview3;//设备类型
    public TextView textview4;//设备地址
    public TextView textview5;//公称通径
    public TextView textview6;//公称压力
    public TextView textview7;//生产商
    public TextView textview8;//安装日期
    public TextView textview9;//设备状态
    public TextView textview10;//巡检项目
    
    public RepairDao  dao;
    
    public SharedPreferenceUtil  sh;
    
    public RepairQueryActivityGroup  act;
    
    public DeviceVo device;
    public Button   button;
    public RepairVo  repair;
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
	
	super.onCreate(savedInstanceState);
	setContentView(R.layout.device_info);
	act=(RepairQueryActivityGroup)getParent();
	sh=new SharedPreferenceUtil(this);
	dao=new RepairDao(this, sh.getValue(XmlNameHelper.SqliteName));
	init();
    }

    //初始化可视化控件
    public void init(){
	textview1=(TextView)findViewById(R.id.device_info_devicename);
	textview2=(TextView)findViewById(R.id.device_info_devicenumber);
	textview3=(TextView)findViewById(R.id.device_info_devicetype);
	textview4=(TextView)findViewById(R.id.device_info_deviceadds);
	textview5=(TextView)findViewById(R.id.device_info_deviceCaliber);
	textview6=(TextView)findViewById(R.id.device_info_devicePressure);
	textview7=(TextView)findViewById(R.id.device_info_producers);
	textview8=(TextView)findViewById(R.id.device_info_InstallationDate);
	textview9=(TextView)findViewById(R.id.device_info_copystatus);
	textview10=(TextView)findViewById(R.id.device_info_deviceitem);
	TextView t=(TextView)findViewById(R.id.device_info_textView10);
	t.setText("维修状态");
	button=(Button)findViewById(R.id.device_info_result);
	button.setText("点击显示维修结果");
	button.setOnClickListener(new OnClickListener() {
	    
	    @Override
	    public void onClick(View v) {
		if(repair==null){
		    return;
		}
		showDialog();
	    }
	});
    }
    
    //初始化数据
   public void initDate(){
	  device=dao.queryRfidDevice(null, act.getDeviceid(),true);
	  if(device==null){
	      return;
	  }else{
	      repair=dao.queryRepair(null,device.getDeviceId());
	  }
	textview1.setText(device.getUseStatus());
	textview2.setText(device.getDeviceNumber());
	textview3.setText(device.getDeviceType());
	textview4.setText(device.getDeviceAdds());
	textview4.setSelected(true);
	textview5.setText(device.getDeviceCaliber());
	textview6.setText(device.getDevicePressure());
	textview7.setText(device.getProducers());
	textview8.setText(device.getInstallationDate());
	textview9.setText(device.getCopystatus());
	textview10.setText(device.getOpenStatus());
    }
   
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {

	
	menu.add(0, ITEM0 + 1, 0, "百度地图");
	menu.add(0, ITEM0 + 2, 0, "查看图文");	
	menu.add(0, ITEM0 + 4, 0, "RFID异常维修");

	menu.findItem(ITEM0 + 1);
	// 获取MenuInflater对象
	// setMenuBackGround();
	return true;

   }

   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
	if (device == null) {
	    Toast.makeText(RepairDeviceInfoActivity.this, "没有设备不能进行些操作", Toast.LENGTH_LONG).show();
	    return false;
	}
	Intent intent=null;
	if (item.getItemId() == ITEM0) {
	    
	} else if (item.getItemId() == (ITEM0 + 1)) {
	    if(device.getLatitude()<=0){
		Toast.makeText(RepairDeviceInfoActivity.this, "当前设备没有地理信息", Toast.LENGTH_SHORT).show();
		return false;
	    }
	    intent=new Intent(RepairDeviceInfoActivity.this,QueryRoutePlanMap.class );
	    intent.putExtra("latitude", device.getLatitude());
	    intent.putExtra("longitude", device.getLongitude());
	    intent.putExtra("devicelistid", device.getDeviceListId());
	    startActivity(intent);
	} else if (item.getItemId() == (ITEM0 + 2)) {
	    intent=new Intent(RepairDeviceInfoActivity.this, GridMainActivity.class);
	    intent.putExtra("deviceid", device.getDeviceId());
	    intent.putExtra("devicelistid", device.getDeviceListId());
	    intent.putExtra("type", "3");
	    intent.putExtra("action", false);
	    startActivityForResult(intent, REQUEST_CODE_BANDING);
	} else if (item.getItemId() == (ITEM0 + 3)) {
//	    intent=new Intent(MaintainDeviceInfoActivity.this, DeviceBandingActivity.class);
//	    intent.putExtra("deviceid", device.getDeviceId());
//	    startActivityForResult(intent, REQUEST_CODE_BANDING);
	} else if (item.getItemId() == (ITEM0 + 4)) {
	    intent=new Intent(RepairDeviceInfoActivity.this, RepairSingleNoRfidActivity.class);
	    intent.putExtra("deviceid", device.getDeviceId());
	    startActivityForResult(intent, REQUEST_CODE_NORFID);
	} 
	return super.onOptionsItemSelected(item);
   }

   public void handleActivityResult(int requestCode, int resultCode, Intent data) {
       
   }
   
 //显示详细信息
   public void showDialog(){
        
        if(repair==null){
            repair=dao.queryRepair(null,device.getDeviceId());
        }
        
	AlertDialog.Builder dialog=new AlertDialog.Builder(this);
	dialog.setTitle("维修结果信息");
	
	dialog.setItems(abc(repair), null);
	
	dialog.setNegativeButton("确定", new DialogInterface.OnClickListener() {
	    
	    @Override
	    public void onClick(DialogInterface dialog, int which) {
		dialog.dismiss();
		
	    }
	});
	
    dialog.create();
    dialog.show();
   }
   
   public String[]  abc(RepairVo  repair){
       List<String>  l=new ArrayList<String>();
       l.add("维修方式: "+repair.getRepairmethod());
       l.add("故障类型: "+fault(repair.getFaultType()));
       l.add("更换部件: "+repair.getReParts());
       l.add("备注: "+repair.getRemark());
       l.add("维修时间: "+repair.getRepairDate());
       String []  s=new String[l.size()];
       for(int i=0;i<l.size();i++){
	   s[i]=l.get(i);
       }
    return s;
       
   }
   
   public String fault(String info){
       System.out.println("----------info----------"+info);
       String[]  s=new String[]{"请选择","环境问题","外观问题","外泄露问题","开关不灵活","内泄露问题","出水问题","压力问题","排气问题"};
       if(TextUtils.isEmpty(info)){
	   return null;
       }
      
       
       String str="";
       String[] ss=info.split(",");
       for(int i=0;i<ss.length;i++){
	   if(ss[i].equals("1")){
	       str=str+s[i]+";";
	   }
	
       }
    return str;
   }
   
    @Override
    protected void onDestroy() {
	dao.close();
	super.onDestroy();
    }
    
  
    
}
